package operation;
import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description：
 */
public class AddBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书....");

        /*if(isFull(bookList)) {
            System.out.println("书架满了....");
            return;
        }*/

       /* if(isFull(bookList)) {
            //扩容的代码 Arrays.copyOf();
        }*/

        //1. 整理书籍的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书籍的名称：");
        String name = scanner.nextLine();

        System.out.println("请输入书籍的作者：");
        String author = scanner.nextLine();

        System.out.println("请输入书籍的价格：");
        int price = scanner.nextInt();
        scanner.nextLine();

        System.out.println("请输入书籍的类型：");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);

        //2.如果书架有这本书 则不能上架
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)) {
                System.out.println("有这本书，可以不上架！！");
                return;
            }
        }
        //3.没有这本书 则放到书籍数组当中
        bookList.setBooks(currentSize,book);

        bookList.setUsedSize(currentSize+1);
    }


    private boolean isFull(BookList bookList) {
        return bookList.getBooks().length ==
                bookList.getUsedSize();
    }
}
